﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;

public partial class Reports_BI_KPI_CashLast7Day : System.Web.UI.Page
{

    #region Members

    StringBuilder sbTbl = new StringBuilder();
    string nl = @"
";
    DataTable tblData;
    DateTime dt = DateTime.Now;
    int TblCurRow = -1;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ret = null;

        oCaption.Text = "Оборот руб. ; Сегодня - " + String.Format("{0:00} / ", dt.Month) + dt.Year;

        ret = MakeData();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        ret = MakeHTML_Table();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
    }

    private void MakeDataTable(ref DataTable tblSrc)
    {
        //tblData = new DataTable("Table");
        //tblData.Columns.Add("Месяц", typeof(string));
    }

    private Exception MakeData()
    {
        Exception ret = null;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);
        ret = FillDataTable();
        if (ret != null) return ret;
        return ret;
    }

    Exception FillDataTable()
    {
        Exception ret = null;
        if (dt.Day <= 1)
            dt = dt.AddDays(-1);
        ret = new Ko2.DB.BI.Reports.Cash().Get(ref tblData, dt, Ko2.DB.BI.Reports.Cash.Cube.KPI);
        return ret;
    }

    Exception MakeHTML_Table()
    {
        Exception ret = null;
        TblCurRow = -1;
        Int64 max = 0;
        string str = "";
        sbTbl.Append(
@"
<script language='javascript'>
//<![CDATA[
function drawChart() {
                var data = new google.visualization.DataTable();
                data.addColumn('string', 'Label');
                data.addColumn('number', 'Value');
                data.addRows([
");

        foreach (DataRow r in tblData.Rows)
        {
            if (r[0].ToString() == "Все")
            {
                foreach (DataColumn c in tblData.Columns)
                {
                    if (c.ColumnName != "Все" && c.ColumnName != "Магазин")
                    {
                        TblCurRow++;
                        Int64 val = Convert.ToInt64(r[c.ColumnName]);
                        string colname = c.ColumnName;
                        DateTime dtColName = DateTime.Parse(c.ColumnName);
                        colname = dtColName.ToShortDateString();
                        if (max < val)
                            max = val;
                        str = @"['" + colname + @"', " + val.ToString() + @"]";
                        if (TblCurRow == 0)
                            sbTbl.Append(str + nl);
                        else
                            sbTbl.Append(nl + @", " + str);
                    }
                }
                break;
            }
        }

        Int64 i10Proc = max / 100 * 10;
        Int64 i10Proc_2 = i10Proc * 2;
        Int64 i10Proc_4 = i10Proc * 4;

        sbTbl.Append(@"
                        ]);

                var options = {
                    width: 1024, height: 480,
                    min: 0, 
                    max: " + max + @",
                    redFrom: " + (max - i10Proc_2) + @", redTo: " + max + @",
                    yellowFrom: " + (max - i10Proc_4) + @", yellowTo: " + (max - i10Proc_2) + @",
                    minorTicks: 5
                };

                var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
                chart.draw(data, options);
    }
//]]>
</script>");

        oBody.Text = sbTbl.ToString();
        return ret;
    }

    Exception TblAddRow(DataRow r)
    {
        Exception ret = null;
        return ret;
    }

}